<template>
  <view class="question-card-container">
    <view class="scene-container" :style="bg_style">
      <view class="top-action-view">
        <div class="title-div">
          <text @click="clickClose" class="close-text">关闭</text>
          <text class="sep">/</text>
          <text class="scene-title">{{scene_name}}</text>
        </div>
      </view>
      <view class="video-container">
        <view class="question-container">
          <text class="title-text" style="margin: auto 40px; text-align: center; line-height: 22px;">{{ questionList[currentIndex].title }}</text>
          <view class="option-container">
            <view class="answer-container" v-if="questionList[currentIndex].type == 0" :style="[questionList[currentIndex].question_id != 15 ? 'margin-left: 35px;' : 'margin-left: 25px;' ]">
              <view v-for="(item, index) in left_list" class="answer-item"
                @click="clickOption(index * 2)">
                <view class="answer-dot"></view>
                <text class="answer-text">{{ item.name }}</text>
                <view class="squar-box"
                  :style="[selected_option == index * 2 ? (questionList[currentIndex].is_allowed ? selected_style : normal_style) : normal_style]">
                </view>
              </view>
            </view>
            <view class="answer-container" v-if="questionList[currentIndex].type == 0" style="margin-left: 45px;">
              <view v-for="(item, index) in right_list" class="answer-item"
                @click="clickOption(index * 2 + 1)">
                <view class="answer-dot"></view>
                <text class="answer-text">{{ item.name }}</text>
                <view class="squar-box"
                  :style="[selected_option == index * 2 + 1 ? (questionList[currentIndex].is_allowed ? selected_style : normal_style) : normal_style]">
                </view>
              </view>
            </view>

            <view class="image-option-container" v-if="questionList[currentIndex].type == 1">
              <view v-for="(item, index) in questionList[currentIndex].options" class="image-option-item" @click="clickOption(index)">
                <image :src="item.url" mode="heightFix" class="option-image"></image>
                <view class="answer-item">
                  <view class="answer-dot"></view>
                  <text class="answer-text">{{ item.name }}</text>
                  <view class="squar-box"
                    :style="[selected_option == index ? (questionList[currentIndex].is_allowed ? selected_style : normal_style) : normal_style]">
                  </view>
                </view>
            
              </view>
            </view>

          </view>

          <view class="action-view">
            <view v-if="questionList[currentIndex].is_allowed" @click="clickSubmit">提交</view>
            <view v-if="!questionList[currentIndex].is_allowed && questionList.length > currentIndex + 1"
              @click="clickNext" >下一题</view>
            <view v-if="!questionList[currentIndex].is_allowed && !(questionList.length > currentIndex + 1)"
              @click="clickClose">关闭</view>
          </view>
          <view class="tips-view" v-if="!questionList[currentIndex].is_allowed">已经答过这题</view>
        </view>
      </view>
    </view>
  </view>

</template>

<script>
import { API, request } from '../../common/request';

export default {
  name: "question-card",
  props: {
    questionList: {
      type: Array,
      default: [],
    },
    scene_name: {
      type: String,
      default: ""
    },
    scene_bg: {
      type: String,
      default: "sida_bg.png"
    }
  },
  data() {
    return {
      normal_style: 'background-color: white;',
      selected_style: 'background-color: red;',
      answerList: null,
      currentIndex: 0,
      selected_option: null,
      allowQuestionList: [],
      audioContext: null,
      left_list: [],
      right_list: [],
      bg_style: "background-image: url('https://oss.hkrin.com/other_img/card_bg/bridgt_bg.png');background-repeat: no-repeat;background-size: contain;"
    }
  },
  computed: {},
  methods: {
    clickSubmit() {
      request({
        path: API.SCENE_QUESTION_SUBMIT,
        params: {
          question_id: this.questionList[this.currentIndex].question_id,
          user_answer: this.questionList[this.currentIndex].options[this.selected_option].name
        }
      }).then((res) => {
        if (res.isSuccess) {
          if (res.data.is_correct) {
            this.audioContext.src = 'https://oss.hkrin.com/audio/right.m4a';
            this.audioContext.play();
          } else {
            this.audioContext.src = 'https://oss.hkrin.com/audio/wrong.m4a';
            this.audioContext.play();
          }
        }
      })
    },

    clickNext() {
      this.selected_option = null;
      this.currentIndex ++;
      this.buildList();
    },

    clickClose() {
      this.$emit("questionEnd");
    },

    buildList() {
      if (this.currentIndex < this.questionList.length) {
        // this.left_list.clear();
        this.left_list.splice(0,this.left_list.length);
        this.left_list.push(this.questionList[this.currentIndex].options[0]);
        this.left_list.push(this.questionList[this.currentIndex].options[2]);

        // this.right_list.clear();
        this.right_list.splice(0,this.right_list.length);
        this.right_list.push(this.questionList[this.currentIndex].options[1]);
        if (this.questionList[this.currentIndex].options.length >= 4) {
          this.right_list.push(this.questionList[this.currentIndex].options[3]);
        }
      }
    },

    precheckQuestionList() {
      request({
        path: API.SCENE_QUESTION_CHECK,
        params: {
          question_ids: Array.from(this.questionList, item => item.question_id)
        }
      }).then((res) => {
        if (res.isSuccess) {
          res.data.forEach((item) => {
            this.questionList.forEach((question) => {
              if (question.question_id == item.question_id) {
                question.is_allowed = item.is_allowed;
              }
            })
          })
          console.log(this.questionList);
        }
      });
    },

    clickOption(option) {
      console.log(option);
      this.selected_option = option;
    },

    createAudio() {
      this.audioContext = uni.createInnerAudioContext();
      this.audioContext.src = '../../static/audio/right.m4a';
      this.audioContext.onPlay(() => {
        console.log('开始播放');
      });
      this.audioContext.onStop(()=> {
        console.log('停止');
      })
      this.audioContext.onEnded(() => {
        console.log('停止');
        if (this.questionList.length > this.currentIndex + 1){
          this.selected_option = null;
           this.currentIndex++;
           this.buildList();
        } else {
          this.$emit("questionEnd");
        } 
      })
    },
  },
  watch: {},
  // 组件周期函数--监听组件挂载完毕
  mounted() {
    this.buildList();
    this.bg_style = "background-image: url('https://oss.hkrin.com/other_img/card_bg/" + this.scene_bg + "');background-repeat: no-repeat;background-size: contain;"
    this.createAudio();
    this.precheckQuestionList();
  },

  unmounted() {
    if (this.audioContext) {
        this.audioContext.stop();
        this.audioContext.destroy();
        this.audioContext = null;
    }
  },
  // 组件周期函数--监听组件数据更新之前
  beforeUpdate() {
    console.log('beforeUpdate');
  },
  // 组件周期函数--监听组件数据更新之后
  updated() {
    console.log('updated');
  },
  // 组件周期函数--监听组件激活(显示)
  activated() {
    console.log('activated');
  },
  // 组件周期函数--监听组件停用(隐藏)
  deactivated() {
    console.log('deactivated');
  },
  // 组件周期函数--监听组件销毁之前
  beforeDestroy() {
    console.log('beforeDestroy');
  },
}

</script>

<style lang="scss">
.question-card-container {
  position: fixed;
  z-index: 99;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.right-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  margin-top: 100rpx;
  margin-bottom: 100rpx;
  justify-content: space-between;
}

.scene-container {
  width: calc(656px * 0.8);
  height: calc(332px * 0.8);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  background-repeat: no-repeat;
  background-size: contain;
}

.top-action-view {
  width: 100%;
  font-size: 14px;
  color: white;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  margin-top: 20px;
  
  .title-div {
    display: flex;
    flex-direction: row;
    align-items: center;
    .close-text {
    margin-left: 20px;
  }

  .sep {
    margin-left: 8px;
    font-size: 24px;

  }
  .scene-title {
    font-family: pangmen;
    margin-left: 10px;
    font-size: 22px;
    letter-spacing: 3px;
  }
  }
}

.background-image {
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
  z-index: -1;
}

.mobile-scene-container {
  position: fixed;
  z-index: 9999;
  margin-top: 200rpx;
  justify-self: center;
  align-self: center;
  width: 700rpx;
  height: 800rpx;
  background-color: rgb(61, 61, 134);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}

.verticel-text {
  height: 330px;
  font-size: 40px;
  word-wrap: break-word;
  width: 40px;
  line-height: 50px;
  color: white;
}

.mobile-text {
  font-size: 40px;
  color: white;
}

.video-container {
  margin-top: 10px;
  width: calc(600px * 0.8) ;
  height: 350px;
}

.mobile-video-container {
  width: 100%;
  height: 56.2%;
}

.question-container {
  margin-top: 0px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.option-container {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.answer-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-around;

}

.answer-item {
  // flex: 1;
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;

  .answer-dot {
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background-color: white;
  }

  .answer-text {
    // font-family: pangmen;
    font-size: 14px;
    font-weight: 600;
    margin-left: 8px;
    color: white;
  }

  .squar-box {
    height: 20px;
    width: 20px;
    background-color: white;
    margin-left: 8px;
  }
}

.action-view {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  width: 100%;
  color: white;
  font-size: 14px;

  view {
    border-radius: 3px;
    padding: 8px 30px 8px 30px;
    background-color: #283E6A;
    box-shadow: 2px 2px 2px rgba(211, 211, 211, 0.214), -2px 2px 2px rgba(211, 211, 211, 0.213), -2px -2px 2px rgba(211, 211, 211, 0.197);
  }
}

.tips-view {
  color: white;
  font-size: 13px;
  margin-top: 8px;
}

.title-text {
  // font-family: pangmen;
  font-size: 16px;
  font-weight: 900;
  color: white;
}

.image-option-container{
  margin-top: 10px;
  display: flex;
  flex-direction: row;
  width: 90%;
  justify-content: space-around;
  
}

.image-option-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.option-image {
  // width: 80px;
  height: 60px;
}
</style>